package cn.oinch.bangbang.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 自定义mvc配置
 *
 * @author: sunHaoMing
 * @date: 18-7-29 下午5:36
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
//
//    private final UserHandlerInterceptor userHandlerInterceptor;
//
//    @Autowired
//    public WebMvcConfig(UserHandlerInterceptor userHandlerInterceptor) {
//        this.userHandlerInterceptor = userHandlerInterceptor;
//    }

    /**
     * 新增FastJsonConverter 配置。指定时间格式化方式。
     *
     * @param converters 现有converter列表
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        /*
        1.需要先定义一个convert转换消息的对象；
        2.添加fastJson的配置信息，比如是否要格式化返回的json数据
        3.在convert中添加配置信息
        4.将convert添加到converters中
         */
        //1.定义一个convert转换消息对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息，比如：是否要格式化返回json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect
        );
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm");
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converters.add(converter);
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        List<String> excludeList = new LinkedList<>();
        excludeList.add("/user/login");
        excludeList.add("/*.html");
        excludeList.add("/category/list");
        excludeList.add("/order/notify");
        excludeList.add("/order/refundNotify");
        excludeList.add("/swagger-resources");
        excludeList.add("/v2/api-docs");
        excludeList.add("/v2/api-docs-ext");
        excludeList.add("/ls/dyu");
        excludeList.add("/webjars/**");
        excludeList.add("/lm/**");
        excludeList.add("/druid/**");
        excludeList.add("/ac/**");
//        InterceptorRegistration addInterceptor = registry.addInterceptor(userHandlerInterceptor);
//        addInterceptor.excludePathPatterns(excludeList);
//        addInterceptor.addPathPatterns("/**");

        super.addInterceptors(registry);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

